<?php
namespace Common\Model;

/**
 * 客栈-设施表
 * @modify trainyao 2015-12-31 创建
 * @index hotelfacility(`isdelete`, `hotelinfo_id`, `facility_id`)
 */

Class HotelFacilityModel extends \Common\Model\BaseModel
{
    protected $trueTableName = 'bk_hotel_facility';
    
    /**
     * 根据酒店获取设施
     * @modify trainyao 2015-12-31  创建
     * @param  integer   $hotelId 客栈id
     * @return array
     */
    public function getInfoByHotel($hotelId) {
        $option = array(
            'isdelete' => NOT_DELETE,
            'hotelinfo_id' => $hotelId
        );
        $fields = 'hotelinfo_id,facility_id';
        
        $data = $this->where($option)->field($fields)->select();
        if (empty($data)) {
            return array();
        }
        return $data;
    }
    
    /**
     * 增加客栈设置(一个或多个)
     * @modify trainyao 2015-12-31  创建
     * @param  integer   $hotelId    客栈id
     * @param  mixin   $facilityId 设施id(s)
     * @return boolean 执行结果
     */
    public function addInfo($hotelId, $facilityId) {
        $data = array();
        $result = null;
        if (!is_array($facilityId) && strpos($facilityId, ',') !== false) {
            $facilityId = explode(',', $facilityId);
        }
        
        if (is_array($facilityId)) {
            foreach ($facilityId as $facilityIdItem) {
                array_push($data, array(
                    'hotelinfo_id' => $hotelId,
                    'facility_id' => $facilityIdItem
                ));
            }
            $result = $this->addAll($data);
        } else {
            $data['hotelinfo_id'] = $hotelId;
            $data['facility_id'] = $facilityId;
            $result = $this->add($data);
        }
        
        return $result == false ? false : true;
    }
    
    /**
     * 删除多个客栈-设施,一个或多个
     * @modify trainyao 2015-12-31  创建
     * @param  integer   $hotelId    客栈id
     * @param  mixin   $facilityId   设施id(s)
     * @return boolean 执行结果
     */
    public function deleteInfoByHotel($hotelId, $facilityId) {
        $option = array(
            'isdelete' => NOT_DELETE,
            'hotelinfo_id' => $hotelId
        );
        if (is_array($facilityId) || strpos($facilityId, ',') !== false) {
            $option['facility_id'] = array(
                'in',
                $facilityId
            );
        } else {
            $option['facility_id'] = $facilityId;
        }
        $data = array(
            'isdelete' => IS_DELETE
        );
        
        return $this->where($option)->save($data) === false ? false : true;
    }
}
